<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

/*
    Function : e107shop_display_current_products
    -----------------------------------------------
    Parameters : $category      The category
    ===============================================
    This function displays a box with a list
    of current products. A category ID can be sent
    to it to retrieve only items for that category.
    ===============================================
*/

function e107shop_display_current_products($category = 0) {
    global $e107shop_db_name, $sql, $ns;
    // Display the categories first
    $display = "<br />".e107shop_display_category_list();
    $display .= "<br><br>";
    // Now display the products
    if($category){
        $query = "category_id='".$category."'";
        if(!$sql->db_Select($e107shop_db_name['category_matrix'], "*", $query." group by product_id")) {
            $ns->tablerender("<center>".lan_admin_menu_products."</center>", 
            "<center>".lan_admin_prod_no_prod_error."</center>");
        } else {    
            $display .= "<center><form name='form1'>
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_products_title."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_products_products.":&nbsp;&nbsp;<select class=\"tbox\" name='p' id='p'>";
                                
            $sql2 = new db;    
            while($row=$sql->db_Fetch()){
                $sql2->db_Select($e107shop_db_name['products'], "*", "ID=".$row["product_id"]);
                while($row2=$sql2->db_Fetch()){
                    $display.="<option value='".$row2["ID"]."'>".$row2["product_name"]."</option>";
                }
            }
        }
        $display.="</option></select>&nbsp;&nbsp;";

        $display.="<input class=\"tbox\" type='button' name='a' value='".lan_global_edit."' 
        OnClick=\"window.location='admin_manage_products.php?a=1&p='
        +document.form1.p.options[document.form1.p.selectedIndex].value\">&nbsp;&nbsp;";
        $display.="<input class=\"tbox\" type='button' name='d' value='".lan_global_delete."' 
        OnClick=\"window.location='admin_manage_products.php?a=2&p='
        +document.form1.p.options[document.form1.p.selectedIndex].value\">";
        $display.="</form>";
        $display.="</td></tr></table>";
    } else {
        $display .= "<center><form name='form1'>
                        <table class='fborder' width='95%'>
                            <tr>
                                <td class='forumheader'>
                                    ".lan_manage_products_title."
                                </td>
                            </tr>
                            <tr>
                                <td class='forumheader2'>
                                    ".lan_manage_products_products.":&nbsp;&nbsp;<select class=\"tbox\" name='p' id='p'>";
        $sql->db_Select($e107shop_db_name['products'], "*", "");
        while($row=$sql->db_Fetch()) {
            $display.="<option value='".$row["ID"]."'>".$row["product_name"]."</option>";
        }

        $display.="</select>&nbsp;&nbsp;<input class=\"tbox\" type='button' name='a' value='".lan_global_edit."' 
        OnClick=\"window.location='admin_manage_products.php?a=1&p='
        +document.form1.p.options[document.form1.p.selectedIndex].value\">&nbsp;&nbsp;";
        $display.="<input class=\"tbox\" type='button' name='d' value='".lan_global_delete."' 
        OnClick=\"window.location='admin_manage_products.php?a=2&p='
        +document.form1.p.options[document.form1.p.selectedIndex].value\">";
        $display.="</form>";
        $display.="</td></tr></table>";
    }

    $ns->tablerender("<center>".lan_admin_menu_products."</center>", $display);
    
}

/*
    Function : e107shop_display_category_list()
    -------------------------------------------
    Parameters : None
    ===========================================
    This function is used to display a dropdown
    list of categories.
    ===========================================
*/

function e107shop_display_category_list() {
    global $e107shop_db_name, $ns;
    
    $sql = new db;
    
    if(!$sql->db_Select($e107shop_db_name['categories'], "*", "active=1")) {
        $ns->tablerender("<center>".lan_global_status."</center>", 
        lan_admin_nocat_error);
    } else {    
        
        $display.="<center><form name='form2'>
                    <table class='fborder' width='95%'>
                        <tr>
                            <td class='forumheader'>
                                ".lan_manage_cats_title."
                            </td>
                        </tr>
                        <tr>
                            <td class='forumheader2'>
                                ".lan_manage_cats_products.":&nbsp;&nbsp;<select class=\"tbox\" name='g' id='g'>
                                <optgroup label='".lan_admin_cat_active."'></optgroup>";
        
        while($row=$sql->db_Fetch()) {    
            if($row["catId"]==$_GET["g"]) {
                $display.="<option selected value='".$row["catId"]."'>".$row["catName"]."</option>";
            } else {
                $display.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
            }
        }
        
        //
        // Inactive groups
        //
        $sql->db_Select($e107shop_db_name['categories'], "*", "active<>1");

        $display .="<optgroup label='".lan_admin_cat_inactive."'></optgroup>";
        while($row=$sql->db_Fetch()) {
            if($row["catId"]==$_GET["g"]) {
                $display.="<option selected value='".$row["catId"]."'>".$row["catName"]."</option>";
            } else {
                $display.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
            }
        }
        
        $display.="</select>&nbsp;&nbsp;<input class=\"tbox\" type='button' name='a' value='".lan_admin_go."' OnClick=\"window.location='admin_manage_products.php?g='+document.form2.g.options[document.form2.g.selectedIndex].value\">";
        $display.="</form>";
        $display.="</td></tr></table>";
    }
    $sql->db_Close();
    return $display;
}

/*
    Function : e107shop_manage_product
    --------------------------------------------------
    Parameters :    $action     The action to do
                    $product    The productid
    ==================================================
    This function displays the form for adding/editing
    a product.
    ===================================================
*/

function e107shop_manage_product($action, $product) {
    global $e107shop_db_name, $sql, $ns, $tp;
    $action = (int) $action;
    $product = $tp->toDB($product);
    if($action==1) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['products'], "*", "ID=".$product);
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $large_image = $row["large_image"];
            $product_price = $row["product_price"];
            $description = $tp->toHTML($row["description"], TRUE);
            $product_name = $row["product_name"];
            $product_active = $row["active"];
            $product_postage = $row["postage_price"];
            $stock_on_hand = $row["stock_on_hand"];
            $product_cost_price = $row["cost_price"];
            $product_sale_price = $row["sale_price"];
            $product_downloadable = $row["downloadable"];
            $download_file = $row["download_file"];
            $go_to_page = $row["function_page"];
            $special_price = $row["special_price"];
            $product_tier = $row["product_tier"];
            $function_name_stripped2 = $go_to_page;
            $customize = $row["customize"];
            $cust_cat = $row["cust_cat"];
            $fname_1 = $row["fname_1"];            
            $fname_2 = $row["fname_2"];            
            $fname_3 = $row["fname_3"];            
            $fname_4 = $row["fname_4"];            
            $fname_5 = $row["fname_5"];            
            $fname_6 = $row["fname_6"];            
            $fname_7 = $row["fname_7"];            
            $fname_8 = $row["fname_8"];            
            $fname_9 = $row["fname_9"];            
            $fname_10 = $row["fname_10"];            
            $fname_11 = $row["fname_11"];            
            $fname_12 = $row["fname_12"];            
            $fname_13 = $row["fname_13"];            
            $fname_14 = $row["fname_14"];            
            $fname_15 = $row["fname_15"];            
            $fname_16 = $row["fname_16"];            
            $fname_17 = $row["fname_17"];            
            $fname_18 = $row["fname_18"];            
            $fname_19 = $row["fname_19"];            
            $fname_20 = $row["fname_20"];            
            $thumb_only = $row["thumb_only"];

            $function_name_stripped=preg_replace("[\((.*?)\)]", "", $go_to_page);
            $go_to_page3 = str_replace($function_name_stripped, "", $go_to_page);
            $go_to_page3 = str_replace("(", "", $go_to_page3);
            $go_to_page3 = str_replace(")", "", $go_to_page3);

        }
        if($product_active==1) {
            $ischecked = "checked";
        }
        
        if($customize==1) {
            $ischecked6 = "checked";
        }

        if($thumb_only==1) {
            $ischecked7 = "checked";
        }

        if($product_downloadable==1) {
            $ischecked3 = "checked";
        }
        $formaction = 1;
    } else {
        $formaction = 2;
    }

    //
    // Display the form
    //
    $display = "<SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"text/javascript\">
    <!--
    function MoveOption(objSourceElement, objTargetElement) {
        var aryTempSourceOptions = new Array();
        var x = 0;
        
        //looping through source element to find selected options
        for (var i = 0; i < objSourceElement.length; i++) {
            if (objSourceElement.options[i].selected) {
                //need to move this option to target element
                var intTargetLen = objTargetElement.length++;
                objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
                objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
            } else {
                //storing options that stay to recreate select element
                var objTempValues = new Object();
                objTempValues.text = objSourceElement.options[i].text;
                objTempValues.value = objSourceElement.options[i].value;
                aryTempSourceOptions[x] = objTempValues;
                x++;
            }
        }
        
        //resetting length of source
        objSourceElement.length = aryTempSourceOptions.length;
        //looping through temp array to recreate source select element
        for (var i = 0; i < aryTempSourceOptions.length; i++) {
            objSourceElement.options[i].text = aryTempSourceOptions[i].text;
            objSourceElement.options[i].value = aryTempSourceOptions[i].value;
            objSourceElement.options[i].selected = false;
        }
    }
    function allSelect() {
        List = document.productform.Enabled;
        if (List.length && List.options[0].value == 'temp') return;
        for (i=0;i<List.length;i++) {
            List.options[i].selected = true;
        }
    }
    //-->
    </SCRIPT>";

    $display .= "<center><br /><form action='".e_SELF."' method='POST' name='productform' ID='productform' OnSubmit='return allSelect();'>
                <table width='95%' class='fborder'>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_global_product_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='product_name' value='".$product_name."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_price.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='product_price' value='".$product_price."'>
                            &nbsp;&nbsp;".lan_admin_prod_price_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_special.": 
                        </td>
                        <td class='forumheader3'>
                            <input type='text' class='tbox' name='special_price' value='$special_price'>
                            &nbsp;&nbsp;".lan_admin_special_info."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_postage.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='postage_price' value='".$product_postage."'>
                            &nbsp;&nbsp;".lan_admin_prod_postage_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_stock.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='stock_on_hand' value='".$stock_on_hand."'>
                            &nbsp;&nbsp;".lan_admin_prod_stock_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_image.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type=text name='large_image' value='".$large_image."'>&nbsp;
                            <input class=\"tbox\" type='button' name='image' 
                            onClick=\"window.open('../library/admin/large_product_image_manager.php','popuppage',
                            'width=700,height=500,top=100,left=100, status=yes, scrollbars=yes');\" 
                            value=\"".lan_admin_prod_choose_image."\">
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_thumb_only.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='thumb_only' ".$ischecked7.">
                            &nbsp;&nbsp;".lan_admin_thumb_only_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_tier.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='product_tier' value='".$product_tier."'>
                            &nbsp;&nbsp;".lan_admin_prod_tier_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_cat.": 
                        </td>
                        <td class='forumheader3'><br />".lan_admin_prod_cat_help."<br /><br />"
                            .e107shop_get_cat_list($product)."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_description.": 
                        </td>
                        <td class='forumheader3'>
                            <textarea class='tbox' name='product_description' rows='25' cols='120'>".$description."</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_active.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='active' ".$ischecked.">
                        </td>
                    </tr>
                    </table>
                    <br /><br />
                    <table width='95%' class='fborder'>
                    <tr>
                        <td colspan='2' class='forumheader'>
                            ".lan_advanced_dl_options."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_download.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='download' ".$ischecked3.">
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_download_file.": 
                        </td>
                        <td class='forumheader3'>
                            ".e107shop_get_file_list($download_file)."
                        </td>
                    </tr>
                    </table>
                    <br /><br />
                    <table width='95%' class='fborder'>
                    <tr>
                        <td colspan='2' class='forumheader'>
                            ".lan_advanced_prod_options."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_customize.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='customize' ".$ischecked6.">
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_cust_fields.":
                        </td>
                        <td class='forumheader3'>
                            <select class='tbox' name='cust_cat'>
                            <option>$cust_cat</option>
                            <option value='1'>1</option>
                            <option value='2'>2</option>
                            <option value='3'>3</option>
                            <option value='4'>4</option>
                            <option value='5'>5</option>
                            <option value='6'>6</option>
                            <option value='7'>7</option>
                            <option value='8'>8</option>
                            <option value='9'>9</option>
                            <option value='10'>10</option>
                            <option value='11'>11</option>
                            <option value='12'>12</option>
                            <option value='13'>13</option>
                            <option value='14'>14</option>
                            <option value='15'>15</option>
                            <option value='16'>16</option>
                            <option value='17'>17</option>
                            <option value='18'>18</option>
                            <option value='19'>19</option>
                            <option value='20'>20</option>
                            </select>                        
                        </td>
                    </tr>
  
                    <tr>
              <td class='forumheader2' width='20%'>
                            Custom Field Names: 
              </td>
              <td class='forumheader3'>
                            ".lan_feature1.": 
                            <input class=\"tbox\" type='text' name='fname_1' value='".$fname_1."'>
                            ".lan_feature2.": 
                            <input class=\"tbox\" type='text' name='fname_2' value='".$fname_2."'>
                            ".lan_feature3.": 
                            <input class=\"tbox\" type='text' name='fname_3' value='".$fname_3."'><br />
                            ".lan_feature4.": 
                            <input class=\"tbox\" type='text' name='fname_4' value='".$fname_4."'>
                            ".lan_feature5.": 
                            <input class=\"tbox\" type='text' name='fname_5' value='".$fname_5."'>
                            ".lan_feature6.": 
                            <input class=\"tbox\" type='text' name='fname_6' value='".$fname_6."'><br />
                            ".lan_feature7.": 
                            <input class=\"tbox\" type='text' name='fname_7' value='".$fname_7."'>
                            ".lan_feature8.": 
                            <input class=\"tbox\" type='text' name='fname_8' value='".$fname_8."'>
                            ".lan_feature9.": 
                            <input class=\"tbox\" type='text' name='fname_9' value='".$fname_9."'><br />
                            ".lan_feature10.": 
                            <input class=\"tbox\" type='text' name='fname_10' value='".$fname_10."'>
                            ".lan_feature11.": 
                            <input class=\"tbox\" type='text' name='fname_11' value='".$fname_11."'>
                            ".lan_feature12.": 
                            <input class=\"tbox\" type='text' name='fname_12' value='".$fname_12."'><br />
                            ".lan_feature13.": 
                            <input class=\"tbox\" type='text' name='fname_13' value='".$fname_13."'>
                            ".lan_feature14.": 
                            <input class=\"tbox\" type='text' name='fname_14' value='".$fname_14."'>
                            ".lan_feature15.": 
                            <input class=\"tbox\" type='text' name='fname_15' value='".$fname_15."'><br />
                            ".lan_feature16.": 
                            <input class=\"tbox\" type='text' name='fname_16' value='".$fname_16."'>
                            ".lan_feature17.": 
                            <input class=\"tbox\" type='text' name='fname_17' value='".$fname_17."'>
                            ".lan_feature18.": 
                            <input class=\"tbox\" type='text' name='fname_18' value='".$fname_18."'><br />
                            ".lan_feature19.": 
                            <input class=\"tbox\" type='text' name='fname_19' value='".$fname_19."'>
                            ".lan_feature20.": 
                            <input class=\"tbox\" type='text' name='fname_20' value='".$fname_20."'>
                            </td>
                    </tr>

                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_prod_go_to_page.": 
                        </td>
                        <td class='forumheader3'>
                            ".lan_product_function_1.": <select name='cur_functions' class='tbox'>
                            <option value=''></option>
                            ".e107shop_get_function_list($go_to_page)."
                            </select><br>
                            ".lan_product_vars.": <input type='text' class='tbox' name='go_to_page' value='$go_to_page3' size=60> <br>
                            ".lan_admin_prod_go_to_info."
                        </td>
                    </tr>

                    <tr>
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='".$formaction."' name='faction'>
                            <input class=\"tbox\" type='hidden' value='".$product."' name='productid'><center>";
    
    if ($action == 1) {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_update."' name='subbed'>";
    } else {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_submit."' name='subbed'>";
    }
    
    $display .= "</center></td></tr></form></table>";

    if ($action == 1) {
        $ns->tablerender("<center>".lan_manage_edit_product."</center>", $display);
    } else {
        $ns->tablerender("<center>".lan_manage_add_product."</center>", $display);
    }
}

/*
    Function : e107shop_get_cat_list
    --------------------------------------------
    Parameters : $product   The productid
    ============================================
    This function is used to grab the select box
    for the categories.
    ============================================
*/

function e107shop_get_cat_list($product) {
    global $e107shop_db_name, $sql;
    $sql2 = new db;
    $display = "<table border=0 align='left'><tr>";
    $enabled = "    <td style='width: 110px; text-align: center;'>
                        ".lan_admin_enabled."<br>
                        <select style='width: 110px' class=\"tbox\" name='Enabled[]' id='Enabled' multiple size=5>";
    
    $query = "SELECT * FROM `".MPREFIX.$e107shop_db_name['category_matrix']."` a 
            LEFT JOIN `".MPREFIX.$e107shop_db_name['categories']."` b ON a.category_id = b.catId 
            WHERE a.product_id = ".$product;

    if($sql->db_Select_gen($query))
    {
        while($row=$sql->db_Fetch())
        {
            $enabled .="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
            $cats .= $row["category_id"] .", ";
        }
        
        $enabled.="</select></td>";

        //
        // Display the Enable/disable buttons
        //
        $middle = '<td style="width: 25px;">&nbsp;</td>
        <td>
        <input class="tbox" style="width:90px;" type="button" name="Enable" value="'.lan_admin_enable.' -&gt;" onClick="MoveOption(this.form.Disabled, this.form.Enabled)"><br>
        <br>
        <input class="tbox" style="width:90px;" type="button" name="Disable" value="&lt;- '.lan_admin_disable.'" onClick="MoveOption(this.form.Enabled, this.form.Disabled)"><br>
         </td>
         <td style="width: 25px;">&nbsp;</td>';

        //
        // Fix the cat string and then select the unused categories
        //
        $disabled ="<td style='width: 110px; text-align: center;'>".lan_admin_disabled."<br><select style='width: 110px' class=\"tbox\" name='Disabled[]' id='Disabled' multiple size=5>";
        $cats = substr("$cats", 0, -2);
        $query2 = "SELECT * FROM `".MPREFIX.$e107shop_db_name['categories']."` WHERE catId Not IN (".$cats.")";
        $sql->db_Select_gen($query2);
        while($row=$sql->db_Fetch())
        {
            $disabled.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
        }
    }
    else
    {
        $enabled .= "</select></td>";
        //
        // Display the Enable/disable buttons
        //
        $middle = '<td style="width: 25px;">&nbsp;</td>
        <td>
        <input class="tbox" style="width:90px;" type="button" name="Enable" value="'.lan_admin_enable.' -&gt;" onClick="MoveOption(this.form.Disabled, this.form.Enabled)"><br>
        <br>
        <input class="tbox" style="width:90px;" type="button" name="Disable" value="&lt;- '.lan_admin_disable.'" onClick="MoveOption(this.form.Enabled, this.form.Disabled)"><br>
         </td>
         <td style="width: 25px;">&nbsp;</td>';

        $disabled ="<td style='width: 110px; text-align: center;'>".lan_admin_disabled."<br><select style='width: 110px;' class=\"tbox\" name='Disabled[]' id='Disabled' multiple size=5>";
        $sql->db_Select($e107shop_db_name['categories'], "*", "");
        while($row=$sql->db_Fetch())
        {
            $disabled.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
        }
    }
    
    $disabled .="</select></td><td>";
    $display .= $disabled.$middle.$enabled."</tr></table>";
    return $display;
}

/*
    Function : e107shop_process_product
    ------------------------------------------------------------------------
    Parameters :    $action         The action to take
                    $product        The productid
                    $l_image        Large Image
                    $cats           The categories
                    $description    The description of the product
                    $stock          The stock of the product
                    $price          The price of the product
                    $postage        The postage price
                    $name           The name of the product
                    $active         If the product is active
                    $downloadable   If the product is a digital download
                    $d_file         The filename of the digital download
                    $special_price  Special price of the product
                    $tier           Normally for sorting, but here thumbsize
                    ...
    ========================================================================
    This function processes the product and adds, deletes or updates it in 
    the database.
    ========================================================================
*/

function e107shop_process_product($action, $product, $l_image, $cats, $description, $stock, $price, 
$postage, $name, $active, $downloadable, $d_file, $special_price, $tier, $go_to_page, $custom, 
$cust_field, $fname_1, $fname_2, $fname_3, $fname_4, $fname_5, $fname_6, $fname_7, $fname_8, 
$fname_9, $fname_10, $fname_11, $fname_12, $fname_13, $fname_14, $fname_15, $fname_16, $fname_17, 
$fname_18, $fname_19, $fname_20, $thumb_only) {
    global $e107shop_db_name, $tp;
    
    if($active!="")
    {
        $active=1;
    }
    else
    {
        $active=2;
    }
    if($downloadable!="")
    {
        $downloadable=1;
    }
    else
    {
        $downloadable=2;
    }
    if($custom!="")
    {
        $custom=1;
    }
    else
    {
        $custom=2;
    }
    if($thumb_only!="")
    {
        $thumb_only=1;
    }
    else
    {
        $thumb_only=2;
    }

    //$tmpstr = $tp->toDB($description);
    //$description = nl2br($tmpstr);
    if ($cust_field=='') $cust_field=0;
    if ($stock=='') $stock=0;
    global $sql, $ns, $scriptDebug;
    if($action==1)
    {
        //
        // edit
        //
        $sql->db_Update($e107shop_db_name['products'], " large_image='".$l_image."', description='".$tp->toDB($description)."', stock_on_hand=".$stock.", product_price='".$price."', postage_price='".$postage."', product_name='".$tp->toDB($name)."', active=".$active.", downloadable=".$downloadable.", download_file='".$d_file."', special_price='".$special_price."', product_tier='".$tier."', function_page='".$go_to_page."', customize='".$custom."', cust_cat=".$cust_field.", fname_1='".$fname_1."', fname_2='".$fname_2."', fname_3='".$fname_3."', fname_4='".$fname_4."', fname_5='".$fname_5."', fname_6='".$fname_6."', fname_7='".$fname_7."', fname_8='".$fname_8."', fname_8='".$fname_8."', fname_9='".$fname_9."', fname_10='".$fname_10."', fname_11='".$fname_11."', fname_12='".$fname_12."', fname_13='".$fname_13."', fname_14='".$fname_14."', fname_15='".$fname_15."', fname_16='".$fname_16."', fname_16='".$fname_16."', fname_17='".$fname_17."', fname_18='".$fname_18."', fname_19='".$fname_19."', fname_20='".$fname_20."', thumb_only='".$thumb_only."' where ID=".$product);

        //
        // delete all the categories
        //
        $sql->db_Delete($e107shop_db_name['category_matrix'], "product_id=".$product);
        
        //
        // Add the new cageory array
        //
        foreach($cats as $cats2)
        {
            $sql->db_Insert($e107shop_db_name['category_matrix'], $product.", ". $cats2, $scriptDebug);
        }

        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center><br />".lan_global_updated."</center>");
    } else {
        //
        // Insert a new product
        //
        if ($name != '') $sql->db_Insert($e107shop_db_name['products'], "0, '".$l_image."', '".$price."', '"
        .$tp->toDB($description)."', '".$tp->toDB($name)."', ".$active.", '".$postage."', '".$stock."', '".$downloadable."', '"
        .$d_file."', '".$special_price."', '".$tier."', '".time()."', '".$go_to_page."', '".$custom."', '"
        .$cust_field."', '".$fname_1."', '".$fname_2."', '".$fname_3."', '".$fname_4."', '".$fname_5."', '"
        .$fname_6."', '".$fname_7."', '".$fname_8."', '".$fname_9."', '".$fname_10."', '".$fname_11."', '"
        .$fname_12."', '".$fname_13."', '".$fname_14."', '".$fname_15."', '".$fname_16."', '".$fname_17."', '"
        .$fname_18."', '".$fname_19."', '".$fname_20."', '".$thumb_only."'");
        
        if ($sql > 0) {
            $text = lan_global_inserted;
        }

        //
        // Add the new cageory array
        //
        $newID = mysql_insert_id();
        foreach($cats as $cats2)
        {
            
            $sql->db_Insert($e107shop_db_name['category_matrix'], $newID.", ". $cats2);
        }
        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center><br />".$text."</center>"); 
    }


}

/*
    Function : e107shop_get_file_list
    ---------------------------------
    Parameters : $download_file_name    The filename of the digital download
    ========================================================================
    This function gets a list of the files in the download folder.
    ========================================================================
*/

function e107shop_get_file_list($download_file_name)
{
    global $shop_settings;
    $d_file = "<select class='tbox' name='download_file'>";
    $d_file .= "<option value=''>Select a Download...</option>";
    //
    // Get the download directory
    //
    foreach (glob($shop_settings["download_dir"]."/*") as $downloads) {
        
        $download_file = str_replace($shop_settings["download_dir"]."/", "", $downloads);
        if($download_file_name==$download_file)
        {
            $d_file .= "<option selected value='".$download_file."'>".$download_file."</option>";
        }
        else
        {
            $d_file .= "<option value='".$download_file."'>".$download_file."</option>";
        }
    }

    $d_file.="</select>";

    return $d_file;
}


function e107shop_get_function_list($go_to_page) {
    global $e107shop_db_name;
    $sql = new db;
    $sql->db_Select($e107shop_db_name['custom'], "*", "r_type=1");
    //
    // explode the possible variables into an array then strip them from the 
    // function name to be able to grab it from the database.
    //
    foreach(explode(", ", "\$theorder, \$thestatus, \$theOnumber, \$thegateway, \$theregion, \$coupon") as $strip)
    {        
        $go_to_page = str_replace($strip, "", $go_to_page);
    }
    $function_name=str_replace(",", "" , $function_name_stripped);
    $go_to_page = preg_replace("[\((.*?)\)]", "", $go_to_page);
    $go_to_page .="()";
    
    while($row5=$sql->db_Fetch())
    {
        if($row5["r_name"] == $go_to_page)
        {
            $displayer .= "<option selected value='".$row5["r_name"]."'>".$row5["r_name"]."</option>";
            
        }
        else
        {
            $displayer .= "<option value='".$row5["r_name"]."'>".$row5["r_name"]."</option>";
        }
    }
    return $displayer;
}
